/*
 * @Descripttion:
 * @version:
 * @Author: lihk
 * @Date: 2022-10-08 12:16:52
 * @LastEditors: lihk
 * @LastEditTime: 2023-12-22 12:19:23
 */
import CryptoJS from "crypto-js";
// import { getToken } from "./auth";
// import { store } from "@/redux";

/**
 * @description:
 * @param {*} headers 请求头对象
 * @param {*} path 请求的路径 例如：common/xt
 * @return {*}
 * @author: lihk
 */
export function getHeaders (path, header) {
  const headers = {}
  headers['Content-Type'] = 'application/json';
  headers.userId = header.userId || 665;
  headers.appId = 1;
  headers.appVer = "1.0.0";
  headers.time = Date.now();
  const str = path + headers.time + headers.userId + "HinteenSeventeen";
  const hash = CryptoJS.MD5(str).toString();
  headers.auth = hash;
  return headers
}
/**
 * @description: 解析加密的函数
 * @param {data} 加密的数据
 * @return {JSON.parse(decStr)} 将其转换为json格式
 * @author: lihk
 */
export function getKeyData (data) {
  const dataArray = CryptoJS.enc.Base64.parse(data);
  //密钥
  const keyArray = CryptoJS.enc.Latin1.parse("HinteenSeventeen");
  // iv
  const dataStr = CryptoJS.enc.Latin1.stringify(dataArray);
  const ivStr = dataStr.slice(0, 16);
  const encStr = dataStr.slice(16);
  const ivArray = CryptoJS.enc.Latin1.parse(ivStr);
  let enArray = CryptoJS.enc.Latin1.parse(encStr);
  enArray = CryptoJS.enc.Base64.stringify(enArray);

  // 加密参数
  const option = {
    iv: ivArray,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  };
  // 解密
  const decrypted = CryptoJS.AES.decrypt(enArray, keyArray, option);
  const decStr = CryptoJS.enc.Utf8.stringify(decrypted);
  return JSON.parse(decStr);
}